<template>
  <div class="categoryFirst">
    <!-- 用户数据 -->
    <div class="tableform" >
         <el-button type="primary" @click="adddialogFormVisible=true">添加分类</el-button>
      <el-table
        border
        ref="multipleTable"
        :data="categoryList"
        tooltip-effect="dark"
        style="width: 100%;margin-top:10px"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
        >
        </el-table-column>
        <el-table-column
          prop="id"
          label="分类的编号"
        >
        </el-table-column>
        <el-table-column
          prop="categoryName"
          label="分类名称"
          show-overflow-tooltip
        >
        </el-table-column>
          <el-table-column
          prop="isDelete"
          label="状态"
          show-overflow-tooltip
        >
        <!-- 状态过滤器 -->
        <template slot-scope="scope">
          <span>{{scope.row.isDelete|capitalize}}</span>
        </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)"
              type="primary"
              icon="el-icon-edit"
              ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <el-pagination
      class="pageination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!-- 添加分类 -->
    <el-dialog title="添加分类" :visible.sync="adddialogFormVisible">
        <el-form :model="addForm">
            <el-input v-model="addForm.categoryName" auto-complete="off" placeholder="请输入分类名称"></el-input>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="adddialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addcategoryFirst">确 定</el-button>
        </div>
    </el-dialog>
    <!-- 编辑分类 -->
      <el-dialog title="添加分类" :visible.sync="editdialogFormVisible">
        <el-form :model="editForm">
            <el-input v-model="editForm.categoryName" auto-complete="off" placeholder="请输入分类名称"></el-input>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="editdialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="editcategoryFirst">确 定</el-button>
        </div>
    </el-dialog>

  </div>
</template>
<script>
import {queryCategoryFirst, addCategoryFirst, editCategoryFirst} from '@/api'
export default {
  data () {
    return {
      categoryList: [{
        categoryName: '',
        id: '',
        isDelete: ''
      }],
      multipleSelection: [],
      //   分页
      page: 1,
      pageSize: 5,
      total: 0,
      //  控制添加分类的显示与隐藏
      adddialogFormVisible: false,
      //  控制修改分类的显示与隐藏
      editdialogFormVisible: false,
      //   添加分类
      addForm: {
        categoryName: ''
      },
      //   编辑分类
      editForm: {
        categoryName: '',
        id: '',
        isDelete: ''
      }
    }
  },
  methods: {
    // 分页
    handleSizeChange (value) {
      console.log(value)
      this.pageSize = value
      this.init()
    },
    handleCurrentChange (value) {
      console.log(value)
      this.page = value
      this.init()
    },
    // 多选
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    // 编辑一级页面数据
    handleEdit (row) {
      // console.log(row)
      //   控制修改分类对话框的显示和隐藏
      this.editdialogFormVisible = true
      this.editForm.categoryName = row.categoryName
      this.editForm.id = row.id
      this.editForm.isDelete = row.isDelete
    },
    // 添加分类
    addcategoryFirst () {
      // 调用接口，实现添加分类的功能
      addCategoryFirst(this.addForm.categoryName)
        .then(res => {
          // console.log(res)
          if (res.success) {
            //   成功,给出提示
            this.$message('分类名称添加成功')
            // 隐藏添加分类对话框
            this.adddialogFormVisible = false
            // 清空数据
            this.addForm.categoryName = ''
            // 重新渲染数据
            this.init()
          } else {
            //   失败,给出提示
            this.$message('分类名称添加失败')
          }
        })
    },
    // 修改分类
    editcategoryFirst () {
      // 调用接口,实现分类的修改
      editCategoryFirst(this.editForm)
        .then(res => {
        //   console.log(res)
          if (res.success) {
            //   成功,给出提示
            this.$message('分类名称修改成功')
            // 隐藏添加分类对话框
            this.editdialogFormVisible = false
            // 重新渲染数据
            this.init()
          } else {
            //   失败,给出提示
            this.$message('分类名称修改失败')
          }
        })
    },
    // 动态渲染数据
    init () {
      // 调用接口渲染数据
      queryCategoryFirst({page: this.page, pageSize: this.pageSize})
        .then(res => {
        //   console.log(res)
          this.categoryList = res.rows
          this.total = res.total
        })
    }
  },
  mounted () {
    this.init()
  },
  // 状态过滤器
  filters: {
    capitalize: function (value) {
      if (value === 1) {
        return '已启用'
      } else if (value === 0) {
        return '已禁用'
      }
    }

  }
}
</script>
<style lang="scss" scoped>
.categoryFirst {
  margin: 10px 0;
  .tableform {
    padding: 30px 20px;
    background-color: #fff;
  }
  .cell {
    text-align: center;
  }
  .pageination {
    margin-top: 15px;
    background-color: #eee;
    padding: 10px 20px;
  }
}
</style>
